package geppetto.phraseHMM.phraseExtraction.extractedphrase.data;

import geppetto.phraseHMM.phraseExtraction.extractedphrase.ExtractedPhrasePair;
import geppetto.phraseHMM.phraseExtraction.extractedphrase.ExtractedPhrasePairData;

import java.util.ArrayList;
import java.util.HashMap;

public class ReorderingGraphData extends ExtractedPhrasePairData{

	int[] _sourceIds;
	int[] _targetIds;
	short[] _sourceStartPos;
	short[] _sourceEndPos;
	short[] _targetStartPos;
	short[] _targetEndPos; // ordered from smallest to largest
	ArrayList<ExtractedPhrasePair> phrasePairs;

	public ReorderingGraphData(int[] ids, int[] ids2,
			short[] startPos, short[] endPos,
			short[] startPos2, short[] endPos2) {
		super();
		_sourceIds = ids;
		_targetIds = ids2;
		_sourceStartPos = startPos;
		_sourceEndPos = endPos;
		_targetStartPos = startPos2;
		_targetEndPos = endPos2;
	}
	
	public ReorderingGraphData(){
		this.phrasePairs = new ArrayList<ExtractedPhrasePair>();
	}
	
	public ReorderingGraphData(ArrayList<ExtractedPhrasePair> phrasePairs) {
		super();
		this.phrasePairs = phrasePairs;
	}

	public ReorderingGraphData(ArrayList<Integer> sourceIdsArray,
			ArrayList<Integer> targetIdsArray,
			ArrayList<Short> sourceStartPosArray,
			ArrayList<Short> sourceEndPosArray,
			ArrayList<Short> targetStartPosArray,
			ArrayList<Short> targetEndPosArray) {
		int length = sourceIdsArray.size();
		_sourceIds = new int[length];
		_targetIds = new int[length];
		_sourceStartPos = new short[length];
		_sourceEndPos = new short[length];
		_targetStartPos = new short[length];
		_targetEndPos = new short[length];
		for(int i = 0; i < length; i++){
			_sourceIds[i] = sourceIdsArray.get(i);
			_targetIds[i] = targetIdsArray.get(i);
			_sourceStartPos[i] = sourceStartPosArray.get(i);
			_sourceEndPos[i] = sourceEndPosArray.get(i);
			_targetStartPos[i] = targetStartPosArray.get(i);
			_targetEndPos[i] = targetEndPosArray.get(i);			
		}
	}

	@Override
	public void merge(
			ArrayList<HashMap<String, ExtractedPhrasePairData>> generalData,
			ArrayList<ExtractedPhrasePairData> data) {
		if(!data.contains(this)){
			data.add(this);
		}
		
		int max=0;
		int maxI=0;
		for(int i = 0; i< data.size(); i++){
			ReorderingGraphData reordData = (ReorderingGraphData)data.get(i);
			if(reordData.get_targetEndPos().length>max){
				maxI = i;
				max = reordData.get_targetEndPos().length;
			}
		}
		ReorderingGraphData maxReordData = (ReorderingGraphData)data.get(maxI);
		_sourceIds = maxReordData.get_sourceIds();
		_targetIds = maxReordData.get_targetIds();
		_sourceStartPos = maxReordData.get_sourceStartPos();
		_targetStartPos = maxReordData.get_targetStartPos();
		_sourceEndPos = maxReordData.get_sourceEndPos();
		_targetEndPos = maxReordData.get_targetEndPos();
		//TODO : merge new data if it exists
	}

	@Override
	public String toString() {
		String ret = "";
		if(phrasePairs==null){
		for(int i = 0; i < _targetEndPos.length; i++){
			ret+=_sourceIds[i]+","+_targetIds[i]+","+_sourceStartPos[i]+","+_sourceEndPos[i]+","+_targetStartPos[i]+","+_targetEndPos[i];
			if(i!=_targetEndPos.length-1){
				ret+="|";
			}
		}
		}
		else{
			for(int i = 0; i < phrasePairs.size(); i++){
				ExtractedPhrasePair epp = phrasePairs.get(i);
				ret+=epp.get_sourceId()+","+epp.get_targetId()+","+epp.getSourceStartPos()+","+epp.getSourceEndPos()+","+epp.getForeignStartPos()+","+epp.getForeignEndPos();
				if(i!=phrasePairs.size()){
					ret+="|";
				}
			}
		}
		return ret;
	}

	public static ExtractedPhrasePairData parseString(String s) {
		String[] phrasesArray = s.split("\\|");
		short[] sourceStartPosArray = new short[phrasesArray.length];
		short[] sourceEndPosArray = new short[phrasesArray.length];
		short[] targetStartPosArray = new short[phrasesArray.length];
		short[] targetEndPosArray = new short[phrasesArray.length];
		int[] sourceIds = new int[phrasesArray.length];
		int[] targetIds = new int[phrasesArray.length];
		for (int i = 0; i < phrasesArray.length; i++){
			String phrase = phrasesArray[i];
			String[] phraseArray = phrase.split(",");
			sourceIds[i]=Integer.parseInt(phraseArray[0]);
			targetIds[i]=Integer.parseInt(phraseArray[1]);
			sourceStartPosArray[i] = Short.parseShort(phraseArray[2]);
			sourceEndPosArray[i] = Short.parseShort(phraseArray[3]);
			targetStartPosArray[i] = Short.parseShort(phraseArray[4]);
			targetEndPosArray[i] = Short.parseShort(phraseArray[5]);			
		}
		return new ReorderingGraphData(sourceIds, targetIds, sourceStartPosArray, sourceEndPosArray, targetStartPosArray, targetEndPosArray);
	}

	public int[] get_sourceIds() {
		return _sourceIds;
	}

	public void set_sourceIds(int[] ids) {
		_sourceIds = ids;
	}

	public int[] get_targetIds() {
		return _targetIds;
	}

	public void set_targetIds(int[] ids) {
		_targetIds = ids;
	}

	public short[] get_sourceStartPos() {
		return _sourceStartPos;
	}

	public void set_sourceStartPos(short[] startPos) {
		_sourceStartPos = startPos;
	}

	public short[] get_sourceEndPos() {
		return _sourceEndPos;
	}

	public void set_sourceEndPos(short[] endPos) {
		_sourceEndPos = endPos;
	}

	public short[] get_targetStartPos() {
		return _targetStartPos;
	}

	public void set_targetStartPos(short[] startPos) {
		_targetStartPos = startPos;
	}

	public short[] get_targetEndPos() {
		return _targetEndPos;
	}

	public void set_targetEndPos(short[] endPos) {
		_targetEndPos = endPos;
	}

	public ArrayList<ExtractedPhrasePair> getPhrasePairs() {
		return phrasePairs;
	}	
}
